<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <link rel="shortcut icon" th:href="@{/favicon.png}" type="image/x-icon">
  <link rel="stylesheet" type="text/css" th:href="@{/res/layui/css/layui.css}">
  <link rel="stylesheet" type="text/css" th:href="@{/res/adminui/dist/css/admin.css}">
  <script type="text/javascript" th:src="@{/res/layui/layui.js}"></script>
  <script type="text/javascript" th:src="@{/res/jquery/jquery.js}"></script>
  <link rel="stylesheet" type="text/css" th:href="@{/css/pear.css}">
  <script type="text/javascript" th:src="@{/js/pear.js}"></script>
</head>
<body class="pear-container">
<div class="layui-card">
  <form class="layui-card-body layui-form">
    <div class="layui-form-item">
      <div class="layui-form-item layui-inline">
        <div class="layui-input-inline">
          <input type="text"
                 name="name"
                 id="name"
                 placeholder="请输入"
                 class="layui-input"
          />
        </div>
      </div>
      <div class="layui-form-item layui-inline">
        <button class="pear-btn pear-btn-md pear-btn-primary"
                lay-submit
                lay-filter="theSearchFilter"
        >
          <i class="layui-icon layui-icon-search"></i>查询
        </button>
      </div>
    </div>
  </form>
</div>
<div class="layui-card">
  <div class="layui-card-body" style="padding-top: 0">
    <table id="theTable" lay-filter="tableFilter"></table>
  </div>
</div>

<script type="text/html" id="topToolbar">
  <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="addEvent">
    <i class="layui-icon layui-icon-add-1"></i>新增
  </button>
  <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemoveEvent">
    <i class="layui-icon layui-icon-delete"></i>删除
  </button>
</script>
<script type="text/javascript" th:inline="javascript">
  const basePath = [[${#httpServletRequest.getContextPath()}]] + "/permission"
</script>
<script type="text/javascript">
  layui.use(['table', 'form', 'jquery', 'common'], function () {
    let table = layui.table
    let form = layui.form
    let $ = layui.jquery
    let common = layui.common

    table.render({
      elem: '#theTable',
      url: basePath + '/list/',
      method: 'post',
      page: true,
      skin: 'line',
      toolbar: '#topToolbar',
      // limit: [10, 20, 50, 100],
      defaultToolbar: [{
        title: '刷新',
        layEvent: 'refreshEvent',
        icon: 'layui-icon-refresh',
      }, 'filter', 'exports'],
      cols: [ [
        {type: 'checkbox'},
        {field: 'serialNumber', align: 'center', title: '序号', type: 'numbers'},
        {
          field: 'name', align: 'center', title: '名称',
          templet: function (data) {
            return "<a style='color:#01AAED;'>"
              + data.name
              + "</a>";
          }
        },
        {field: 'code', align: 'center', title: '权限码'},
        {field: 'icon', align: 'center', title: '图标'},
        {field: 'link', align: 'center', title: '链接'},
        {field: 'createTime', align: 'center', title: '创建时间'},
        {field: 'modifyTime', align: 'center', title: '更新时间'}
      ] ]
    });
    //表格中工具栏
    table.on('tool(tableFilter)', function (obj) {
      if (obj.event === 'editEvent') {
        layer.open({
          type: 2,
          title: '修改',
          shade: 0.1,
          area: ['500px', '400px'],
          content: basePath + '/edit/' + obj.data.id
        });
      } else if (obj.event === 'configEvent') {
        layer.open({
          type: 2,
          title: '配置',
          shade: 0.1,
          area: ['500px', '400px'],
          content: basePath + '/config/' + obj.data.id
        });
      }
    });
    //表格顶部工具栏
    table.on('toolbar(tableFilter)', function (obj) {
      if (obj.event === 'addEvent') {
        layer.open({
          type: 2,
          title: '新增',
          shade: 0.1,
          area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '400px'],
          content: basePath + '/add'
        });
      } else if (obj.event === 'refreshEvent') {
        table.reload('theTable');
      } else if (obj.event === 'batchRemoveEvent') {
        window.batchRemove(obj);
      }
    });
    //搜索
    form.on('submit(theSearchFilter)', function (data) {
      table.reload('theTable', {
        where: data.field
      })
      return false;
    });
    //批量删除
    window.batchRemove = function (obj) {
      const checkIds = common.checkField(obj, 'id');
      if (checkIds === "") {
        layer.msg("未选中数据", {
          icon: 3,
          time: 1000
        });
        return false;
      }
      layer.confirm('确定要删除', {
        icon: 3,
        title: '提示'
      }, function (index) {
        layer.close(index);
        let loading = layer.load();
        $.ajax({
          url: basePath + "/del/" + checkIds,
          dataType: 'json',
          type: 'post',
          success: function (result) {
            layer.close(loading);
            if (result.code === 1) {
              layer.msg(result.msg, {
                icon: 1,
                time: 1000
              }, function () {
                table.reload('theTable');
              });
            } else {
              layer.msg(result.msg, {
                icon: 2,
                time: 1000
              });
            }
          }
        })
      });
    }
  })
</script>
</body>
</html>